package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

	@Autowired
	private UserAccountMapper userAccountMapper;

	@Autowired
	private UserAccountDetailMapper userAccountDetailMapper;

	@Autowired
	private RechargeInfoMapper rechargeInfoMapper;

	@Override
	public void initUserAccount(Long userId) {
		//创建对象
		UserAccount userAccount = new UserAccount();
		userAccount.setUserId(userId);
		userAccount.setTotalAmount(new BigDecimal("1000"));
		userAccount.setAvailableAmount(new BigDecimal("1000"));
		userAccount.setTotalIncomeAmount(new BigDecimal("1000"));
		//初始化金额
		userAccountMapper.insert(userAccount);
	}

	@Override
	public BigDecimal getAvailableAmount(Long userId) {
		//	select available_amount from user_account where user_id = ?
		return userAccountMapper.selectOne(new LambdaQueryWrapper<UserAccount>().eq(UserAccount::getUserId, userId).select(UserAccount::getAvailableAmount)).getAvailableAmount();
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int checkAndDeduct(AccountDeductVo accountDeductVo) {
		//调用服务层方法
		int count = userAccountMapper.checkAndDeduct(accountDeductVo.getUserId(), accountDeductVo.getAmount());
		//判断
		if (count == 0){
			throw new GuiguException(222, "余额不足");
		}
		//记录明细
		this.addUserAccountDetail(accountDeductVo.getUserId(),"检查与扣减金额", SystemConstant.ACCOUNT_TRADE_TYPE_MINUS,accountDeductVo.getAmount(),accountDeductVo.getOrderNo());
		return count;
	}

	private void addUserAccountDetail(Long userId, String title, String tradeType, BigDecimal amount, String orderNo) {
		//	创建对象
		UserAccountDetail userAccountDetail = new UserAccountDetail();
		userAccountDetail.setUserId(userId);
		userAccountDetail.setTitle(title);
		userAccountDetail.setTradeType(tradeType);
		userAccountDetail.setAmount(amount);
		userAccountDetail.setOrderNo(orderNo);
		//	保存数据
		userAccountDetailMapper.insert(userAccountDetail);
	}

	@Override
	public void rechargePaySuccess(String orderNo) {
		//查询对象
		RechargeInfo rechargeInfo = rechargeInfoMapper.selectOne(new LambdaQueryWrapper<RechargeInfo>().eq(RechargeInfo::getOrderNo, orderNo));
		//判断
		if (rechargeInfo != null && SystemConstant.ORDER_STATUS_PAID.equals(rechargeInfo.getRechargeStatus())){
			return;
		}
		rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_PAID);
		//更新数据
		rechargeInfoMapper.updateById(rechargeInfo);
	}
}
